from flask import url_for
from flask_restful import Resource

from model.crud import QueryOneInstance, QueryMultipleInstances
from model.tables import User, Blog
from restful.data import DataUid
from restful.error import RenderUserNotExist
from restful.request import Get
from restful.response import ResponseSuccess
from restful.session import login_required,data


class PersonalDetail(Resource):
    @login_required
    @data(DataUid())
    def get(self,data):
        user = QueryOneInstance(User,{'id':data['uid']})
        if not user:
            return RenderUserNotExist(data['uid'])
        userdetail_data = Get(url_for('userdetail', uid=user.id, _external=True))
        passed_posts=QueryMultipleInstances(Blog,{'uid':data['uid'],'checked':True,'passed':True})
        return ResponseSuccess(data={
            **userdetail_data,
            'blogs':[{
                'pid':i.id,
                'title':i.title,
                'sort':i.sort,
                'time':i.time,
            }for i in passed_posts]
        })